#define _CRT_SECURE_NO_WARNINGS 1
#include "string.h"
enum STRING
{
	EXIT,
	INSERT,
	ASSIGN,
	CPY,
	CMP,
	CAT,
	SUBSTRING,
	PRINTF,
	REPLACE,
	FIND,
	LEN,
	ERASE,
	SHOW
};
void show()
{
	printf("|-------------------------------|\n");
	printf("|----------String 操作----------|\n");
	printf("|-------------------------------|\n");
	printf("|        0.退出程序             |\n");
	printf("|        1.字符串的插入         |\n");
	printf("|        2.字符串赋值           |\n");
	printf("|        3.字符串复制           |\n");
	printf("|        4.判断字符串是否相等   |\n");
	printf("|        5.字符串的拼接         |\n");
	printf("|        6.取子串操作           |\n");
	printf("|        7.打印字符串           |\n");
	printf("|        8.字符串的替换         |\n");
	printf("|        9.字符串的查找         |\n");
	printf("|        10.字符串的长度        |\n");
	printf("|        11.删除操作            |\n");
	printf("|        12.显示菜单            |\n");
	printf("|-------------------------------|\n");
}
void StringTest()
{
	int input = 0;
	String s;
	StringInit(&s);
	show();
	do
	{
		printf("请输入选项:>");
		scanf("%d", &input);
		switch (input)
		{
			case EXIT:
				printf("退出程序!!!\n");
				break;
			case INSERT:
			{
				//清理输入缓冲区
				char c = '\0';
				while ((c = getchar()) != '\n' && c != EOF);
				String s1;
				StringInit(&s1);
				printf("请输入要插入的字符串:>");
				char ch[100] = "\0";
				//用gets来获取,因为gets能获取到空格
				fgets(ch, 100, stdin);
				*(ch + strlen(ch) - 1) = '\0';
				StringAssign(&s1, ch);
				printf("请输入要插入主串的位置,不能超过主串长度%d:>", StringLen(&s));
				int pos = 0;
				scanf("%d", &pos);
				//调用接口
				StringInsert(&s, &s1, pos);
			}
				break;
			case ASSIGN:
			{
				char c = '\0';
				while ((c = getchar()) != '\n' && c != EOF);
				printf("请输入要插入的字符串:>");
				char str[100] = "\0";
				fgets(str,100,stdin);
				*(str + strlen(str) - 1) = '\0';
				StringAssign(&s, str);
			}
				break;
			case CPY:
			{
				//清理输入缓冲区
				char c = '\0';
				while ((c = getchar()) != '\n' && c != EOF);
				String s1;
				StringInit(&s1);
				char ch[100] = "\0";
				printf("请输入要拷贝到主串的字符串:>");
				fgets(ch, 100, stdin);
				*(ch + strlen(ch) - 1) = '\0';
				StringAssign(&s1, ch);
				StringCpy(&s, &s1);
			}
				break;
			case CMP:
			{
				char c = '\0';
				while ((c = getchar()) != '\n' && c != EOF);
				String s1;
				StringInit(&s1);
				char ch[100] = "\0";
				printf("请输入要和主串比较的字符串:>");
				fgets(ch, 100, stdin);
				*(ch + strlen(ch) - 1) = '\0';
				StringAssign(&s1, ch);
				if (!StringCmp(&s, &s1)) printf("两个字符串相等\n");
				else printf("两个字符串不相等\n");
				
			}
			break;
			case CAT:
			{
				char c = '\0';
				while ((c = getchar()) != '\n' && c != EOF);
				String s1;
				StringInit(&s1);
				printf("请输入要往主串后面拼接的字符串:>");
				char ch[100] = "\0";
				fgets(ch, 100, stdin);
				*(ch + strlen(ch) - 1) = '\0';
				StringAssign(&s1, ch);
				StringCat(&s, &s1);
			}
			break;
			case SUBSTRING:
			{
				String s1;
				StringInit(&s1);
				printf("请输入要取的开始位置以及长度,开始位置+长度不能超过%d否则断言为生效:>\n", s.len - 1);
				int start = 0, count = 0;
				printf("开始位置:>");
				scanf("%d", &start);
				printf("长度:>");
				scanf("%d", &count);
				StringSubstring(&s, &s1, start, count);
				printf("取出的字符串为:>");
				StringPrintf(&s1);
			}
			break;
			case PRINTF:
			{
				StringPrintf(&s);
			}
			break;
			case REPLACE:
			{
				char c = '\0';
				while ((c = getchar()) != '\n' && c != EOF);
				String s1;
				StringInit(&s1);
				printf("请输入要替换的字符串(替换长度以替换个数为主):>");
				char ch[100] = "\0";
				fgets(ch, 100, stdin);
				*(ch + strlen(ch) - 1) = '\0';
				StringAssign(&s1, ch);
				printf("请输入要替换主串中的起始位置以及替换个数,起始位置不能大于等于字符串的长度%d,替换个数不能超过替换的字符串的长度%d,否则断言生效:>\n", StringLen(&s), StringLen(&s1));
				int start = 0, count = 0;
				printf("起始位置:>");
				scanf("%d", &start);
				printf("替换个数:>");
				scanf("%d", &count);
				StringReplace(&s, &s1, start, count);
			}
			break;
			case FIND:
			{
				char c = '\0';
				while ((c = getchar()) != '\n' && c != EOF);
				String s1;
				StringInit(&s1);
				char str[100] = "\0";
				printf("请输入要查找的字符串:>");
				fgets(str, 100, stdin);
				*(str + strlen(str)-1) = '\0';
				StringAssign(&s1, str);
				int find = StringFind(&s, &s1, 0);
				printf("%d", find);
				if (find >= 0)
					printf("输入的子串在字符串中存在,在下标为%d位置\n", find);
				else printf("输入的子串在字符串中不存在!!!\n");
			}
			break;
			case LEN:
				printf("字符串的长度为:%d\n", StringLen(&s));
			break;
			case ERASE:
			{
				printf("请输入要删除的起始位置以及个数,注意起始位置+个数不能超过字符串的长度%d,否则断言生效:>\n",StringLen(&s));
				int start = 0, count = 0;
				printf("起始位置:>");
				scanf("%d", &start);
				printf("长度:>");
				scanf("%d", &count);
				StringErase(&s, start, count);
			}
			break;
			case SHOW:
				show();
				break;
			default:
				printf("输入错误,请重新输入!!!\n");
				break;
		}
	} while (input);
	//调用销毁字符串的函数来释放内存
	StringDestroy(&s);
}
int main()
{
	printf("王浩 ZB2405905254 物联网24-2\n");
	StringTest();
	return 0;
}